import dayjs from 'dayjs'
import { detail } from '@/api/work/filling-cycle'
const quarterOfYear = require('dayjs/plugin/quarterOfYear')
const isBetween = require('dayjs/plugin/isBetween')
dayjs.extend(isBetween)
dayjs.extend(quarterOfYear)

// 格式输出季度
export function quarterFormat(year, currentQuarter = 1) {
  // 季度基数
  let baseQuarter = 4
  let quarterList = []
  const chineseNumerals = ['一', '二', '三', '四']
  // 是否当前年
  if (dayjs().year() == year) {
    currentQuarter = dayjs().quarter()
    baseQuarter = currentQuarter
  }

  for (let i = 0; i < baseQuarter; i++) {
    let quarterItem = {
      label: `第${chineseNumerals[i]}季度`,
      value: i + 1
    }
    quarterList.push(quarterItem)
  }
  return { quarter: currentQuarter, quarterList }
}

// 季度对应月份
export function quarterToMonth(quarter) {
  let month = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12]
  ]
  return month[quarter - 1]
}

// 校验当前季度工作内容是否可编辑
export function checkQuarterWorkEdit(query) {
  return new Promise((resolve, reject) => {
    detail(query)
      .then(res => {
        let { startDate, endDate } = res.rows[0]
        // 判断是否之间
        let isBetween = dayjs().isBetween(startDate, endDate, 'day', '[]')
        resolve(isBetween)
      })
      .catch(() => {
        resolve(false)
      })
  })
}
